package com.mytrip.base.action;

import java.net.URLDecoder;
import java.util.Locale;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.MessageSource;
import org.springframework.context.MessageSourceAware;

import com.mytrip.webapp.filter.CtxSessionBag;
import com.mytrip.webapp.session.SessionBagImpl;
import com.opensymphony.xwork2.ActionSupport;


/**
 * 所有Struts Action的类应该都从本类继承
 * 
 * @author evan
 * @version 0.1, 2008-3-9
 * @since   JDK1.6
 */
public abstract class StrutsActionAdapter 
		extends ActionSupport implements BaseAction<SessionBagImpl>,
		MessageSourceAware{
	
	public static String GO_BACK = "goBack";
	
	public static String RESULT_MESSAGE = "resultMessage";
	
	public static String BASE_ERROR = "baseError";
	
	public static String BACKEND_LOGIN = "backendLogin";
	
	protected final Log log = LogFactory.getLog(getClass());

	protected String backUrl;//跳转目标url
	
	protected String resultMessage;//操作提示消息
	
	protected Boolean isEdit = false;
	
	protected Boolean isSave =false;
	
	protected Boolean isCopy = false;
	
	private MessageSource messageSource;
	
	public SessionBagImpl getSessionBag() {
		return CtxSessionBag.getSessionBag();
	}
	
	public MessageSource getMessageSource() {
		return messageSource;
	}

	public void setMessageSource(MessageSource messageSource) {
		this.messageSource = messageSource;
	}

	public String getBackUrl() {
		if(backUrl != null){
			try {
	//			return URLEncoder.encode(backUrl, "UTF8").replaceAll("%2F","/").replaceAll("%3D", "=").replaceAll("%3F","?").replaceAll("%26","&");
				return URLDecoder.decode(backUrl.trim(),"UTF-8");
			} catch(Exception e) {
				log.error("ERROR:::",e);
			}
		}
		return backUrl;
	}
	public String getBackUrlValue() {
		return backUrl;
	}

	public void setBackUrl(String backUrl) {
		this.backUrl = backUrl;
	}

	public Boolean getIsEdit() {
		return isEdit;
	}

	public void setIsEdit(Boolean isEdit) {
		this.isEdit = isEdit;
	}

	public Boolean getIsCopy() {
		return isCopy;
	}

	public void setIsCopy(Boolean isCopy) {
		this.isCopy = isCopy;
	}

	public Boolean getIsSave() {
		return isSave;
	}

	public void setIsSave(Boolean isSave) {
		this.isSave = isSave;
	}

	public String getResultMessage() {
		return resultMessage;
	}

	public void setResultMessage(String resultMessage) {
		this.resultMessage = resultMessage;
	}
	
	public String getText(String key){
		Locale l=getSessionBag()==null?null:getSessionBag().getLocale();
		return this.getMessageSource().getMessage(key, null,l );
	}
	
	public String getText(String key, Object[] params){
		Locale l=getSessionBag()==null?null:getSessionBag().getLocale();
		return this.getMessageSource().getMessage(key, params, l);
	}
}
